package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.BaseFormMessageMapper;
import com.zjdiepu.www.dao.third.BaseToMessageMapper;
import com.zjdiepu.www.ibiz.third.IBaseFormMessageService;
import com.zjdiepu.www.model.third.dto.BaseFormMessage;
import com.zjdiepu.www.model.third.dto.BaseToMessage;
import com.zjdiepu.www.model.third.ext.BaseFormMessageExt;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年04月13日 16时14分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseFormMessageServiceImpl extends BaseService implements IBaseFormMessageService {
	public static final Logger logger = LoggerFactory.getLogger(BaseFormMessageServiceImpl.class);

	@Autowired
	private BaseFormMessageMapper baseFormMessageMapper;
	
	@Autowired
	private BaseToMessageMapper baseToMessageMapper;
	
	@Override
	public void saveUpdateAndDelete(BaseFormMessage baseFormMessage, OperateType operType) {
		switch (operType) {
		case add:
			baseFormMessage.initInsertData(baseFormMessage.getModifier(), baseFormMessage.getModifierName());
			baseFormMessageMapper.insert(baseFormMessage);
			break;
		case edit:
			baseFormMessage.initUpdateData(baseFormMessage.getModifier(), baseFormMessage.getModifierName());
			// 影响的记录数
			int updateCount = baseFormMessageMapper.updateByPrimaryKeySelective(baseFormMessage);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseFormMessage.initDeleteData(baseFormMessage.getModifier(), baseFormMessage.getModifierName());
			// 影响的记录数
			int deleteCount = baseFormMessageMapper.updateByPrimaryKeySelective(baseFormMessage);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseFormMessage> selectByExt(BaseFormMessageExt baseFormMessageExt) {
		return baseFormMessageMapper.selectByExt(baseFormMessageExt);
	}
	
	@Override
	public int countByExt(BaseFormMessageExt baseFormMessageExt){
		return baseFormMessageMapper.countByExt(baseFormMessageExt);
	}
	
	@Override
	public BaseFormMessage selectById(String id) {
		return baseFormMessageMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseFormMessageMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseFormMessage baseFormMessage) {
		return baseFormMessageMapper.countRefined(baseFormMessage);
	}

	@Override
	public BaseFormMessage selectRefined(BaseFormMessage baseFormMessage) {
		return baseFormMessageMapper.selectRefined(baseFormMessage);
	}

	@Override
	public boolean repeat(BaseFormMessage baseFormMessage) {
		int count = countRefined(baseFormMessage);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseFormMessage.getId())) {
			return true;
		}
		BaseFormMessage dbItem = selectRefined(baseFormMessage);
		if(null == dbItem) {
			return false;
		}
		return !baseFormMessage.getId().equals(dbItem.getId());
	}

	/* 
	* 所属公司：稻普控股集团
	* @author zhangpp
	* <p>Title: save</p> 
	* <p>Description: </p> 
	* @param baseFormMessage
	* @param baseToMessage 
	* @see com.api.party.service.IBaseFormMessageService#save(com.api.party.entity.model.BaseFormMessage, com.api.party.entity.model.BaseToMessage) 
	* @version V1.0
	*/
	@Override
	public void save(BaseFormMessage baseFormMessage,
			BaseToMessage baseToMessage) {
		
		baseFormMessage.initInsertData(baseFormMessage.getModifier(), baseFormMessage.getModifierName());
		baseFormMessageMapper.insert(baseFormMessage);
		
		baseToMessage.initInsertData(baseToMessage.getModifier(), baseToMessage.getModifierName());
		baseToMessageMapper.insert(baseToMessage);
	}
}
